﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentGIS.Class;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;

namespace ComponentGIS.View
{
    public partial class FormQueryBySpatial : Form
    {
        #region 字段属性
        IMap pMap;
        IMapControl2 pMapControl;
        #endregion

        #region 构造器
        public FormQueryBySpatial(IMapControl2 pMapControl)
        {
            InitializeComponent();
            this.pMapControl = pMapControl;
            pMap = pMapControl.Map;
            List<string> layerNameList = BasicClass.GetFeatureLayerNameList(pMap);
            checkedListBoxTargetLayers.Items.AddRange(layerNameList.ToArray());
            comboBoxSourceLayer.DataSource = layerNameList;
            comboBoxSourceLayer.SelectedIndex = 0;
            comboBoxSpatialRel.SelectedIndex = 0;
            comboBoxMethod.SelectedIndex = 0;
        }
        #endregion

        #region 事件
        private void checkBoxSelectable_CheckedChanged(object sender, EventArgs e)
        {
            checkedListBoxTargetLayers.Items.Clear();
            List<string> layerNameList = new List<string>();
            if (checkBoxSelectable.Checked == true)
                layerNameList = BasicClass.GetSelectableLayerNameList(pMap);
            else
                layerNameList = BasicClass.GetFeatureLayerNameList(pMap);
            checkedListBoxTargetLayers.Items.AddRange(layerNameList.ToArray());
        }

        private void comboBoxSourceLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            IFeatureLayer pFeaLyr = (IFeatureLayer)pMap.get_Layer(comboBoxSourceLayer.SelectedIndex);
            IFeatureSelection pFeaSelection = (IFeatureSelection)pFeaLyr;
            if (pFeaSelection.SelectionSet.Count != 0)
                checkBoxUseSelected.Enabled = true;
            else
                checkBoxUseSelected.Enabled = false;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            SelectFeaturesBySpatial();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            SelectFeaturesBySpatial();
            this.Close();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 方法
        private void SelectFeaturesBySpatial()
        {
            try
            {
                //1. 获取参数
                //1.1 选择方式
                esriSelectionResultEnum selectMethod;
                switch (comboBoxMethod.SelectedItem.ToString())
                {
                    case "创建新选择集":
                        selectMethod = esriSelectionResultEnum.esriSelectionResultNew;
                        break;
                    case "添加到当前选择集":
                        selectMethod = esriSelectionResultEnum.esriSelectionResultAdd;
                        break;
                    case "从当前选择集中删除":
                        selectMethod = esriSelectionResultEnum.esriSelectionResultXOR;
                        break;
                    case "从当前选择集中选择":
                        selectMethod = esriSelectionResultEnum.esriSelectionResultAnd;
                        break;
                    default:
                        selectMethod = esriSelectionResultEnum.esriSelectionResultNew;
                        break;
                }
                //1.2 空间关系
                esriSpatialRelEnum spatialRel;
                switch (comboBoxSpatialRel.SelectedItem.ToString())
                {
                    case "相交(Intersects)":
                        spatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                        break;
                    case "包含(Contains)":
                        spatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                        break;
                    case "被包含(Within)":
                        spatialRel = esriSpatialRelEnum.esriSpatialRelWithin;
                        break;
                    case "穿过(Crosses)":
                        spatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
                        break;
                    case "相接(Touches)":
                        spatialRel = esriSpatialRelEnum.esriSpatialRelTouches;
                        break;
                    default:
                        spatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                        break;
                }
                //1.3 源图层的几何体
                IFeatureLayer pFeaSourceLyr = (IFeatureLayer)pMap.get_Layer(comboBoxSourceLayer.SelectedIndex);
                int count1 = pMapControl.Map.SelectionCount;
                IFeatureSelection pFeaSourceSel = pFeaSourceLyr as IFeatureSelection;
                IGeometry pGeo = null;
                if (checkBoxUseSelected.Checked)
                    pGeo = BasicClass.GetGeoUnion(pFeaSourceSel);
                //1.4 目标图层&执行查询
                for (int i = 0; i < checkedListBoxTargetLayers.CheckedItems.Count; i++)
                {
                    string sLyrName = checkedListBoxTargetLayers.CheckedItems[i].ToString();
                    IFeatureLayer pFeaTargetLyr = BasicClass.GetFeatureLayer(pMap, sLyrName);
                    IFeatureSelection pFeaTargetSel = (IFeatureSelection)pFeaTargetLyr;
                    bool OK = BasicClass.SelectFeatures
                        (selectMethod, spatialRel, pGeo, pFeaTargetLyr, pMap as IActiveView);
                    if (!OK)
                    {
                        MessageBox.Show("图层【{0}】查找失败！", pFeaTargetLyr.Name);
                        return;
                    }
                }
                //定位选择集
                ICommand cmd = new ControlsZoomToSelectedCommandClass();
                cmd.OnCreate(pMapControl);
                cmd.OnClick();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion
    }
}
